package details

import "fmt"

type FamilyAccount struct {
	account string
	pwd     string
	balance float64
}

type Details struct {
	id                   int
	item                 string
	changeMoney, balance float64
}

var detail []*Details
var id int
var key int
var flag bool

func (fa *FamilyAccount) SetFamilyAccount(account, pwd string, balance float64) *FamilyAccount {
	return &FamilyAccount{account, pwd, balance}
}

func (fa *FamilyAccount) GetAccount() {
	var pwd string
	fmt.Println("请输入密码:")
	fmt.Scanln(&pwd)
	if fa.pwd == pwd {
		fmt.Printf("你的账户为:%v余额为:%v\n", fa.account, fa.balance)
	} else {
		fmt.Println("输入有误")
	}
}

func (fa *FamilyAccount) Menu() {
	for !flag {
		fmt.Println("-----家庭收支软件----")
		fmt.Println("	1、收支明细")
		fmt.Println("	2、登记收入")
		fmt.Println("	3、登记支出")
		fmt.Println("	4、退出软件")
		fmt.Println("			请输入选项1-4")
		fmt.Scanln(&key)
		switch key {
		case 1:
			fa.Detail()
		case 2:
			fa.Income()
		case 3:
			fa.Expend()
		case 4:
			fa.Exit()
		default:
			fmt.Println("输入选项有误")
		}
	}
}

func (fa *FamilyAccount) Detail() {
	fmt.Println("收支明细:")
	fmt.Println("id\t项目\t金额\t余额")
	if len(detail) == 0 {
		fmt.Printf("暂时没有收支明细,你的初始资金为:%v\n\n", fa.balance)
	} else {
		for _, item := range detail {
			fmt.Printf("%v\t%v\t%v\t%v\n", item.id, item.item, item.changeMoney, item.balance)
		}
		fmt.Println()
	}
}

func (fa *FamilyAccount) Income() {
	id++
	var item string
	var change float64
	fmt.Println("收入项目为:")
	fmt.Scanln(&item)
	fmt.Println("金额为:")
	fmt.Scanln(&change)
	fa.balance = fa.balance + change
	detail = append(detail, &Details{id, item, change, fa.balance})
	fmt.Println("登记成功!")
}

func (fa *FamilyAccount) Expend() {
	id++
	var item string
	var change float64
	fmt.Println("支出项目为:")
	fmt.Scanln(&item)
	fmt.Println("金额为:")
	fmt.Scanln(&change)
	fa.balance = fa.balance - change
	detail = append(detail, &Details{id, item, change, fa.balance})
	fmt.Println("登记成功!")
}

func (fa *FamilyAccount) Exit() {
	var str string
	fmt.Println("确定要退出吗")
	fmt.Println("y/n")
	fmt.Scanln(&str)
	if str == "y" {
		fmt.Println("Exit successfully")
		flag = true
	}
}
